# -*- coding: UTF-8 -*-


import pandas as pd
from selenium import webdriver, common

'''
基金经理
基金经理网址：http://fundf10.eastmoney.com/jjjl_0062279.html
根据基金的代码获取基金经理的基本信息：
getfundmanagerinfo - 基金的经理列表
getmanagefundlist - 基金经理的信息列表

# selenium模块浏览器静默状态下运行

from selenium import webdriver, common
import time

option = webdriver.ChromeOptions()
option.add_argument('headless')
#这里是重点，增加一个参数即可实现在不打开浏览器的情况下完成系列操作
browser = webdriver.Chrome(chrome_options=option)

url = 'https://www.baidu.com'
browser.get(url)
time.sleep(1)
lst = browser.find_elements_by_xpath('//*[@id="lh"]/a[4]')
print(lst)
for i in lst:
    print(i.text)

time.sleep(3)
browser.close()

[不启动浏览器运行selenium 的webdriver：HtmlUnit Driver(python)](https://www.cnblogs.com/x666-6/p/9103626.html)
[selenium中的三种等待方式-基于python](https://blog.csdn.net/Monster_czy/article/details/89891848)
#添加智能等待
driver.implicitly_wait(30)               
#implicitly_wait()方法比 sleep() 更加智能，后者只能选择一个固定的时间的等待，前者可以在一个时间范围内智能的等待
'''

class HTMLToDataFrame(object):

    def __init__(self, code_fund):

        self.code_fund = code_fund
        self.url = 'http://fundf10.eastmoney.com/jjjl_' + code_fund + '.html'    # http://fundf10.eastmoney.com/jjjl_519760.html
        option = webdriver.ChromeOptions()
        option.add_argument('headless')
        # 这里是重点，增加一个参数即可实现在不打开浏览器的情况下完成系列操作
        # browser = webdriver.Chrome(chrome_options=option)
        driver = webdriver.Chrome(chrome_options=option, executable_path='../tools/chromedriver.exe')
        driver.get(self.url)
        driver.implicitly_wait(5)
        self.html = driver.page_source
        driver.close()

    def get_tables(self, dict_attrs):

        tbs = pd.read_html(self.html, attrs=dict_attrs, encoding='utf-8', header=0)
        return tbs


def main():
    #
    dict_attrs = {"class": "w782 comm  jloff"}
    dict_attrs2 = {"class": "w782 comm jloff"}
    html_hm = HTMLToDataFrame('519760')
    tbs_h = html_hm.get_tables(dict_attrs)
    ls = []
    for r in tbs_h[0]["任职期间"]:
        r_txt = str(r)
        r_txt = r_txt.rstrip('天')
        if '年又' in r_txt:
            r_ls = r_txt.split('年又')
            r_int = int(r_ls[0]) * 365 + int(r_ls[1])
        else:
            r_int = int(r_txt)
        ls.append(r_int)

    # 删除列要加axis=1，默认是删除行的
    tb_h = tbs_h[0].drop('任职期间', axis=1)
    tb_h.insert(3, '任职天数', ls)  # 插入一列
    # 写入csv文件，index=False 表示不保存行索引
    tb_h.to_csv('../data/testdata.csv', index=False)

    # tbs_hm = html_hm.get_tables(dict_attrs2)
    # for tb_hm in tbs_hm:
    #     print('tb_hm:\n', tb_hm, '\n')




if __name__ == '__main__':

    # main()
    tb_h = pd.read_csv("../data/testdata.csv", header=0)
    print('tb_h:\n', tb_h, '\n')
    print(tb_h.dtypes)
    # ls = []
    # for r in tb_h["任职期间"]:
    #     r_txt = str(r)
    #     r_txt = r_txt.rstrip('天')
    #     if '年又' in r_txt:
    #         r_ls = r_txt.split('年又')
    #         r_int = int(r_ls[0]) * 365 + int(r_ls[1])
    #     else:
    #         r_int = int(r_txt)
    #     ls.append(r_int)
    # # tb_h['任职天数'] = ls
    # # 删除列要加axis=1，默认是删除行的
    # tb_h = tb_h.drop('任职期间', axis=1)
    # tb_h.insert(3, '任职天数', ls)  # 插入一列
    # print(tb_h)
    # print(tb_h.dtypes)

    # for name in tb_h.iloc[0, 2].split(" "):
    #     print('name = ', name)
    #     for i in range(1, tb_h.shape[0]):
    #         if name in tb_h.iloc[i, 2]:
    #             print(tb_h.iloc[i, 3])





    # `for tb_h in tbs_h:
    #     print('tb_h:\n', tb_h, '\n')`
        # print('基金经理: ', tb_h.iloc[0, 2], '\n')
        # for name in tb_h.iloc[0, 2].split(" "):
        #     print("name = ", name)